/**
 * Created by dell on 2018/12/7.
 */
const http = require('http');
const path = require('path');
const querystring = require('querystring');
const scoreData = require('./scores.json');
const template = require('art-template');
//启动服务器
http.createServer((req,res)=>{
    //路由 （请求路径+请求方式）
    //查询成绩的入口   /query
    if(req.url.startsWith('/query') && req.method == 'GET'){
        let content =  template(path.join(__dirname,'view','index.art'),{});
        res.end(content);
    }else if(req.url.startsWith('/score') && req.method == 'POST'){
        //获取成绩的结果  /score
        let pdata = '';
        req.on('data',(chunk)=>{
            pdata+=chunk;
        });
        req.on('end',()=>{
            let obj = querystring.parse(pdata);
            let result = scoreData[obj.code];
            let content = template(path.join(__dirname,'view','result.art'),result);
            res.end(content);
        });
    }else if(req.url.startsWith('/all') && req.method == 'GET'){
        //全部成绩 -----新添加一个路由

    }
}).listen(3000,()=>{
    console.log('running.....');
});

